#include <Windows.h>
#include <tchar.h>

bool CompareClassName(HWND hWnd, const TCHAR* szClassName)
{
	bool bChecked = false;
	if (NULL != szClassName)
	{
		TCHAR szChildClassName[MAX_PATH];
		GetClassName(hWnd, szChildClassName, MAX_PATH);
		if (NULL != szChildClassName && 0 == _tcsicmp(szChildClassName, szClassName))
		{
			bChecked = true;
		}
	} else {
		bChecked = true;
	}

	return bChecked;
}

bool CompareWindowName(HWND hWnd, const TCHAR* szWndName)
{
	bool bChecked = false;
	if (NULL != szWndName)
	{
		TCHAR szChildWndName[MAX_PATH];
		GetWindowText(hWnd, szChildWndName, MAX_PATH);
		if (NULL != szChildWndName && 0 == _tcsicmp(szChildWndName, szWndName))
		{
			bChecked = true;
		}
	} else {
		bChecked = true;
	}

	return bChecked;
}

bool CompareCtrlID(HWND hWnd, int nCtrlID)
{
	bool bChecked = false;
	if (-1 != nCtrlID)
	{
		bChecked = (nCtrlID == GetDlgCtrlID(hWnd));
	} else {
		bChecked = true;
	}

	return bChecked;
}

bool FindChildFromTreeWnd(HWND& hChildWnd, HWND hTreeRoot, const TCHAR* szClassName, const TCHAR* szWndName, int nWndID)
{
	if (NULL != hTreeRoot)
	{
		HWND hChild = GetWindow(hTreeRoot, GW_CHILD);
		do {
			TCHAR szTmp[MAX_PATH] = _T(" ");
			_stprintf(szTmp, _T("0x%0X\n"), hChild);
			OutputDebugString(szTmp);

			if (CompareClassName(hChild, szClassName) && CompareWindowName(hChild, szWndName) && CompareCtrlID(hChild, nWndID))
			{
				hChildWnd = hChild;
				break;
			}

			if (NULL != GetWindow(hChild, GW_CHILD))
			{
				FindChildFromTreeWnd(hChildWnd, hChild, szClassName, szWndName, nWndID);
			}
			
		} while (hChild = GetWindow(hChild, GW_HWNDNEXT));
	}

	return (NULL != hChildWnd);
}

void main()
{
	HWND hQQZoneWnd = FindWindow(_T("QQBattle_MainFrame"), NULL);

	HWND hChildWnd = NULL;
	if (FindChildFromTreeWnd(hChildWnd, hQQZoneWnd, _T("Button"), NULL, 0x3F4)) //_T("SysTreeView32"), _T(", 0);  _T("Title")
	{
		TCHAR szTmp[MAX_PATH];
		_stprintf(szTmp, _T("Found Wnd: 0x%0X\n"), hChildWnd);
		OutputDebugString(szTmp);
	}
	

	system("pause");
	return;
}